**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/inv_sup_status_tab.log, text replace
set more off
 
local cutoff = 50
local seed = 9999 
local case_lb = `cutoff'

local covarsmin d_mo_* d_dist_*
local covars vic_female_any vic_white_any acc_white acc_male acc_old cat_3 cat_4 d_mo_* d_dist_* arrests_pre complaints_pre force_pre any_prior_complaint tenure
 
 
*****************
**BRING IN CASE STATUS + COMPLAINTS DATA AND APPLY DATA RESTRICTION
***************** 
 
do ${rep_root}/code/data_restrict.do
 
*****************
**MERGE WITH OUTCOMES DATA
***************** 
merge 1:1 cr_id acc_id using ${rep_root}/data/outcomes, keep(3) nogen

 
*****************
**DEFINE TREATMENT AND MAKE COVARIATES
***************** 
 

**Drop investigators without enough cases
tab inv_id
sort inv_id 
by inv_id: gen inv_ct = _N
drop if inv_ct<`case_lb'
tab inv_id intake_yr

**Define some covariates
gen po = acc_rank == "POLICE OFFICER"
gen minor_inj = vic_inj_1<=1
gen vic_old = (year(inc_complaint_dt_1) - vic_byr_oldest)>=35 & !missing(vic_byr_oldest)
gen acc_old = acc_age>=35 & !missing(acc_age)
gen cat_3 = acc_cat==3
gen cat_4 = acc_cat==4
 
   
  

forvalues i = 1/25{
	gen d_dist_`i' = inc_district_1==`i'
}
forvalues yr = 2006/2008{
	gen d_yr_`yr' = intake_yr==`yr'

	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
	
}
forvalues yr = 2014/2018{
	gen d_yr_`yr' = intake_yr==`yr'
	
	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
}

gen d_noaff = finding_cd=="NO AFFIDAVIT"
gen d_unfound = finding_cd=="UNFOUNDED"
gen d_nosus = finding_cd=="NOT SUSTAINED"
gen d_exon = finding_cd=="EXONERATED"
gen d_sus = finding_cd=="SUSTAINED"
gen d_addlinv = finding_cd=="ADDITIONAL INVESTIGATION REQUESTED"

qui foreach var in vic_female_any vic_white_any acc_white acc_male acc_old cat_3 cat_4 arrests_pre complaints_pre force_pre any_prior_complaint tenure arrests_postc1_3 serious_postc1_3 complaints_postc1_3 arrests_postc4_12 serious_postc4_12 complaints_postc4_12 arrests_posty2 serious_posty2 complaints_posty2 d_noaff d_unfound d_nosus d_exon d_sus d_addlinv{
	sum `var'
	*local temp_m = r(mean)
	local ss_`var'_m : di %6.3f r(mean)
	*local temp_sd = r(sd)
	local ss_`var'_sd : di %6.3f r(sd)
}

sort mid
by mid: gen mid_ct = _N
by mid: gen mid_mark = _n==1
tab mid_mark
sum mid_ct if mid_mark==1
tab mid_ct if mid_mark==1
drop mid_ct mid_mark

sort cr_id
by cr_id: gen cr_ct = _N
by cr_id: gen cr_mark = _n==1
tab cr_mark
sum cr_ct if cr_mark==1
tab cr_ct if cr_mark==1
drop cr_ct cr_mark

**Define treatment
 

gen treat_affidavit = finding_cd != "NO AFFIDAVIT" 
gen treat_sustain = finding_cd == "SUSTAINED"   

gen era = inrange(intake_yr, 2006, 2009) 

foreach t in treat_affidavit treat_sustain{

**Create LOM
capture drop resid mo_resid cr_resid mo_cr_resid cr_ct mo_cr_ct inv_tot_resid mo_inv_tot_resid case mo_case inv_ct2 mo_inv_ct2
qui reg `t' `covars'
*qui reg `t' d_mo_*
predict resid, residuals

sort cr_id
by cr_id: egen cr_resid = total(resid)
by cr_id: gen cr_ct = _N

*sort inv_id intake_yr intake_mo
sort inv_id intake_yr
gen case = 1
by inv_id intake_yr: egen inv_tot_resid = total(resid)
by inv_id intake_yr: egen inv_ct2 = total(case)
gen lom_`t' = (inv_tot_resid - cr_resid)/(inv_ct2-cr_ct)

**Just month effects
qui reg `t' d_mo_*
predict mo_resid, residuals

sort cr_id
by cr_id: egen mo_cr_resid = total(mo_resid)
by cr_id: gen mo_cr_ct = _N

*sort inv_id intake_yr intake_mo
sort inv_id intake_yr
gen mo_case = 1
by inv_id intake_yr: egen mo_inv_tot_resid = total(mo_resid)
by inv_id intake_yr: egen mo_inv_ct2 = total(mo_case)
gen mo_lom_`t' = (mo_inv_tot_resid - mo_cr_resid)/(mo_inv_ct2 - mo_cr_ct)

sort inv_id
by inv_id: sum lom_`t'
}
 

*****************
**STORE FIRST STAGE
*****************  

**First stage
foreach t in treat_affidavit treat_sustain{

sum `t'
local first_mean = r(mean)

reg `t' lom_`t' `covars', cluster(inv_id)
local first = _b[lom_`t']
local first_se = _se[lom_`t']
local first_N = e(N)
boottest lom_`t', weight(webb) nograph seed(`seed')
local first_f = r(t)^2
local first_p = r(p)

local first : di %6.3f `first'
local first_f : di %6.3f `first_f'

local first_mean_`t' : di %6.3f `first_mean'
local first_`t' : di %6.3f `first'
local first_se_`t' : di %6.3f `first_se'
local first_p_`t' : di %6.3f `first_p'
glo first_star_`t' = cond(`first_p_`t'' <0.01, "***", cond(`first_p_`t'' <0.05, "**", cond(`first_p_`t'' <0.1, "*", "")))
local first_f_`t' : di %6.3f `first_f'
local first_N_`t' : di %6.0f `first_N'
 
}
 

gen inc_dow = dow(inc_start_dt_1)
forvalues d = 0/6{
	gen d_inc_dow_`d' = inc_dow==`d'
}

gen any_p_c = any_prior_complaint
 
 
*****************
**PRODUCE GRAPH OF TIME TO RESOLUTION
*****************   
  
preserve
keep if  lom_treat_affidavit!=.

gen duration = (inc_closed_dt_1 - intake_dt)/30  
sum duration if finding == "SUSTAINED", d
sum duration if finding == "NOT SUSTAINED" | finding == "UNFOUNDED"| finding == "EXONERATED", d
sum duration, d
 sum treat_affidavit, d
hist duration, xtitle("Case Duration in Months for All Cases", size(medlarge)) ytitle("Fraction", size(medlarge)) graphregion(fcolor(white) color(white) icolor(white)) frac width (2) scheme(s1color) xlabel(0 (12) 72)
graph export ${rep_root}/output/figures/duration_hist_all.png, replace  
hist duration if finding == "NO AFFIDAVIT", xtitle("Case Duration in Months for Cases with No Affidavit", size(medlarge)) ytitle("Fraction", size(medlarge)) graphregion(fcolor(white) color(white) icolor(white)) frac width (2) scheme(s1color) xlabel(0 (12) 72)
graph export ${rep_root}/output/figures/duration_hist_noaff.png, replace 
hist duration if finding == "SUSTAINED", xtitle("Case Duration in Months for Sustained Cases", size(medlarge)) ytitle("Fraction", size(medlarge)) graphregion(fcolor(white) color(white) icolor(white)) frac width (2) scheme(s1color) xlabel(0 (12) 72)
graph export ${rep_root}/output/figures/duration_hist_sus.png, replace 
hist duration if finding == "NOT SUSTAINED" | finding == "UNFOUNDED"| finding == "EXONERATED", xtitle("Case Duration in Months for Negative Findings", size(medlarge)) ytitle("Fraction", size(medlarge)) graphregion(fcolor(white) color(white) icolor(white)) frac width (2) scheme(s1color) xlabel(0 (12) 72)
graph export ${rep_root}/output/figures/duration_hist_neg.png, replace
restore
**************************
**OLS RESULTS
**************************

foreach treatvar in treat_affidavit treat_sustain {
preserve 


//INITIATE TABLE TO STORE RESULTS

texdoc init ${rep_root}/output/tables/inv_sup_main_`treatvar'.tex, replace force
tex \begin{tabular}{l*{10}{c}}
tex \hline\hline
tex                     &\multicolumn{3}{c}{Complaints}&\multicolumn{3}{c}{Arrests}&\multicolumn{3}{c}{Type 1 Arrests} \\
tex \hline
tex                     &\multicolumn{1}{c}{$<$ Month 3}&\multicolumn{1}{c}{Months 4-12}&\multicolumn{1}{c}{Year 2}&\multicolumn{1}{c}{$<$ Month 3}&\multicolumn{1}{c}{Months 4-12}&\multicolumn{1}{c}{Year 2}&\multicolumn{1}{c}{$<$ Month 3}&\multicolumn{1}{c}{Months 4-12}&\multicolumn{1}{c}{Year 2} \\


texdoc close


	**************************
	**CREATE COMPLIER WEIGHTS
	**************************

reg `treatvar' `covars'  
predict p_risk, xb

_pctile p_risk, nq(8)
gen p_octile = 1 if p_risk<=r(r1)
replace p_octile = 2 if p_risk>r(r1) & p_risk<=r(r2)
replace p_octile = 3 if p_risk>r(r2) & p_risk<=r(r3)
replace p_octile = 4 if p_risk>r(r3) & p_risk<=r(r4)
replace p_octile = 5 if p_risk>r(r4) & p_risk<=r(r5)
replace p_octile = 6 if p_risk>r(r5) & p_risk<=r(r6)
replace p_octile = 7 if p_risk>r(r6) & p_risk<=r(r7)
replace p_octile = 8 if p_risk>r(r7) 

forvalues i = 1/8{
	gen r`i' = p_octile==`i'
}

_pctile lom_`treatvar', nq(100)
return list r99 r1

gen p1 = r(r1)
gen p99 = r(r99)

gen complier_weight_`treatvar' = .
foreach var of varlist r1 r2 r3 r4 r5 r6 r7 r8 {
reg `treatvar' lom_`treatvar'  `covars' if `var'==1
gen frac_complier_`var' = (_b[_cons] + _b[lom_`treatvar']*p99) - (_b[_cons] + _b[lom_`treatvar']*p1)  if `var'==1
egen frac_`var' = mean(`var') 
replace complier_weight_`treatvar' = frac_complier_`var'/frac_`var' if `var'==1 & complier_weight==.
}

drop frac* r1 r2 r3 r4 r5 r6 r7 r8 p_octile p_risk



 
	
local varlist  complaints_postc1_3 complaints_postc4_12 complaints_posty2 arrests_postc1_3 arrests_postc4_12 arrests_posty2 type1_arrests_postc1_3 type1_arrests_postc4_12 type1_arrests_posty2
  
  
**Create Tables

 


**OLS + Reduced FORM
**Loop over outcome variables
foreach var of local varlist{

 
//OLS WITH JUST FES
 reg `var' `treatvar' `covarsmin', cluster(inv_id)
	
	
	matrix v1`var' = e(V)
	local se1`var' = sqrt(v1`var'[1,1])
	
	local beta1`var' = _b[`treatvar']
	boottest `treatvar', weight(webb)  nograph seed(`seed')
	local p1`var' = r(p)
 
	local beta1`var' : di %6.3f `beta1`var''
	local se1`var' : di %6.3f `se1`var''
	local p1`var' : di %6.3f `p1`var'' 
	glo star1_`var' = cond(`p1`var'' <0.01, "***", cond(`p1`var'' <0.05, "**", cond(`p1`var'' <0.1, "*", "")))

	
//OLS WITH ALL CONTROLS	


  reg `var' `treatvar' `covars', cluster(inv_id)
 
	matrix v2`var' = e(V)
	local se2`var' = sqrt(v2`var'[1,1])
	
	local beta2`var' = _b[`treatvar']
	boottest `treatvar', weight(webb)  nograph seed(`seed')
	local p2`var' = r(p)
 
	local beta2`var' : di %6.3f `beta2`var''
	local se2`var' : di %6.3f `se2`var''
	local p2`var' : di %6.3f `p2`var'' 
	glo star2_`var' = cond(`p2`var'' <0.01, "***", cond(`p2`var'' <0.05, "**", cond(`p2`var'' <0.1, "*", "")))
	
	
//OLS WITH COMPLIER WEIGHTING 


 reg `var' `treatvar' `covars' [w=complier_weight], cluster(inv_id)

	matrix v3`var' = e(V)
	local se3`var' = sqrt(v3`var'[1,1])
	
	local beta3`var' = _b[`treatvar']
	boottest `treatvar', weight(webb)  nograph seed(`seed')
	local p3`var' = r(p)
 
	local beta3`var' : di %6.3f `beta3`var''
	local se3`var' : di %6.3f `se3`var''
	local p3`var' : di %6.3f `p3`var'' 
	glo star3_`var' = cond(`p3`var'' <0.01, "***", cond(`p3`var'' <0.05, "**", cond(`p3`var'' <0.1, "*", "")))
	
	//control for the other lom

/*
**Standardize LOMs

sum lom_treat_affidavit, d
replace lom_treat_affidavit = lom_treat_affidavit/ `r(sd)'

sum lom_treat_sustain, d
replace lom_treat_sustain = lom_treat_sustain/ `r(sd)'
	
	
*/
	
//REDUCED FORM	
	
 if "`treatvar'" == "treat_affidavit" {
 reg `var' lom_`treatvar' lom_treat_sustain `covars' , cluster(inv_id)
 }
  if "`treatvar'" == "treat_sustain" {
 reg `var' lom_`treatvar' lom_treat_affidavit `covars' , cluster(inv_id)
 }
 
 
 
	matrix v4`var' = e(V)
	local se4`var' = sqrt(v4`var'[1,1])
	
	local beta4`var' = _b[lom_`treatvar']
	boottest lom_`treatvar', weight(webb)  nograph seed(`seed')
	local p4`var' = r(p)
 
	local beta4`var' : di %6.3f `beta4`var''
	local se4`var' : di %6.3f `se4`var''
	local p4`var' : di %6.3f `p4`var'' 
	glo star4_`var' = cond(`p4`var'' <0.01, "***", cond(`p4`var'' <0.05, "**", cond(`p4`var'' <0.1, "*", "")))


}

 
**************************
**OUTPUT OLS RESULTS
**************************
if "`treatvar'" == "treat_affidavit" {
texdoc init ${rep_root}/output/tables/inv_sup_main_`treatvar'.tex, append force  
tex \hline 
tex \textbf{OLS} \textit{FEs} & `beta1complaints_postc1_3'${star1_complaints_postc1_3} & `beta1complaints_postc4_12'${star1_complaints_postc4_12} &`beta1complaints_posty2'${star1_complaints_posty2} & `beta1arrests_postc1_3'${star1_arrests_postc1_3} & `beta1arrests_postc4_12'${star1_arrests_postc4_12} &`beta1arrests_posty2'${star1_arrests_posty2} & `beta1type1_arrests_postc1_3'${star1_type1_arrests_postc1_3} & `beta1type1_arrests_postc4_12'${star1_type1_arrests_postc4_12} &`beta1type1_arrests_posty2'${star1_type1_arrests_posty2}   \\
tex         &(`se1complaints_postc1_3') &(`se1complaints_postc4_12') & (`se1complaints_posty2') &(`se1arrests_postc1_3') &(`se1arrests_postc4_12') & (`se1arrests_posty2') &(`se1type1_arrests_postc1_3') &(`se1type1_arrests_postc4_12') & (`se1type1_arrests_posty2')   \\
tex  &[`p1complaints_postc1_3'] &[`p1complaints_postc4_12'] & [`p1complaints_posty2'] &[`p1arrests_postc1_3'] &[`p1arrests_postc4_12'] & [`p1arrests_posty2'] &[`p1type1_arrests_postc1_3'] &[`p1type1_arrests_postc4_12'] & [`p1type1_arrests_posty2']   \\

tex \textit{+Controls}  & `beta2complaints_postc1_3'${star2_complaints_postc1_3} & `beta2complaints_postc4_12'${star2_complaints_postc4_12} &`beta2complaints_posty2'${star2_complaints_posty2} & `beta2arrests_postc1_3'${star2_arrests_postc1_3} & `beta2arrests_postc4_12'${star2_arrests_postc4_12} &`beta2arrests_posty2'${star2_arrests_posty2} & `beta2type1_arrests_postc1_3'${star2_type1_arrests_postc1_3} & `beta2type1_arrests_postc4_12'${star2_type1_arrests_postc4_12} &`beta2type1_arrests_posty2'${star2_type1_arrests_posty2}   \\
tex          &(`se2complaints_postc1_3') &(`se2complaints_postc4_12') & (`se2complaints_posty2') &(`se2arrests_postc1_3') &(`se2arrests_postc4_12') & (`se2arrests_posty2') &(`se2type1_arrests_postc1_3') &(`se2type1_arrests_postc4_12') & (`se2type1_arrests_posty2')   \\
tex   &[`p2complaints_postc1_3'] &[`p2complaints_postc4_12'] & [`p2complaints_posty2']& [`p2arrests_postc1_3'] &[`p2arrests_postc4_12'] & [`p2arrests_posty2'] &[`p2type1_arrests_postc1_3'] &[`p2type1_arrests_postc4_12'] & [`p2type1_arrests_posty2']   \\

tex \textit{Complier wt}  & `beta3complaints_postc1_3'${star3_complaints_postc1_3} & `beta3complaints_postc4_12'${star3_complaints_postc4_12} &`beta3complaints_posty2'${star3_complaints_posty2} & `beta3arrests_postc1_3'${star3_arrests_postc1_3} & `beta3arrests_postc4_12'${star3_arrests_postc4_12} &`beta3arrests_posty2'${star3_arrests_posty2} & `beta3type1_arrests_postc1_3'${star3_type1_arrests_postc1_3} & `beta3type1_arrests_postc4_12'${star3_type1_arrests_postc4_12} &`beta3type1_arrests_posty2'${star3_type1_arrests_posty2}   \\
tex          &(`se3complaints_postc1_3') &(`se3complaints_postc4_12') & (`se3complaints_posty2') &(`se3arrests_postc1_3') &(`se3arrests_postc4_12') & (`se3arrests_posty2') &(`se3type1_arrests_postc1_3') &(`se3type1_arrests_postc4_12') & (`se3type1_arrests_posty2')   \\
tex   &[`p3complaints_postc1_3'] &[`p3complaints_postc4_12'] & [`p3complaints_posty2'] &[`p3arrests_postc1_3'] &[`p3arrests_postc4_12'] & [`p3arrests_posty2'] &[`p3type1_arrests_postc1_3'] &[`p3type1_arrests_postc4_12'] & [`p3type1_arrests_posty2']   \\

texdoc close
}

texdoc init ${rep_root}/output/tables/inv_sup_main_`treatvar'.tex, append force  
tex \hline 
tex \textbf{RF}  & `beta4complaints_postc1_3'${star4_complaints_postc1_3} & `beta4complaints_postc4_12'${star4_complaints_postc4_12} &`beta4complaints_posty2'${star4_complaints_posty2} & `beta4arrests_postc1_3'${star4_arrests_postc1_3} & `beta4arrests_postc4_12'${star4_arrests_postc4_12} &`beta4arrests_posty2'${star4_arrests_posty2} & `beta4type1_arrests_postc1_3'${star4_type1_arrests_postc1_3} & `beta4type1_arrests_postc4_12'${star4_type1_arrests_postc4_12} &`beta4type1_arrests_posty2'${star4_type1_arrests_posty2}   \\
tex          &(`se4complaints_postc1_3') &(`se4complaints_postc4_12') & (`se4complaints_posty2')& (`se4arrests_postc1_3') &(`se4arrests_postc4_12') & (`se4arrests_posty2') &(`se4type1_arrests_postc1_3') &(`se4type1_arrests_postc4_12') & (`se4type1_arrests_posty2')   \\
tex   &[`p4complaints_postc1_3'] &[`p4complaints_postc4_12']& [`p4complaints_posty2']& [`p4arrests_postc1_3'] &[`p4arrests_postc4_12'] & [`p4arrests_posty2'] &[`p4type1_arrests_postc1_3'] &[`p4type1_arrests_postc4_12'] & [`p4type1_arrests_posty2']   \\
  
 
texdoc close
 

**************************
**OUTPUT IV RESULTS
**************************

  
 
 
**Create Tables

**SECOND STAGE
local varlist  complaints_postc1_3 complaints_postc4_12 complaints_posty2 arrests_postc1_3 arrests_postc4_12 arrests_posty2 type1_arrests_postc1_3 type1_arrests_postc4_12 type1_arrests_posty2 
foreach var of local varlist{
	
	
	
	//control for the other lom
	
 if "`treatvar'" == "treat_affidavit" {
 ivreg `var' `covars' lom_treat_sustain (`treatvar'=lom_`treatvar'), cluster(inv_id)
 }
  if "`treatvar'" == "treat_sustain" {
 ivreg `var' `covars' lom_treat_affidavit (`treatvar'=lom_`treatvar'), cluster(inv_id)
 }
 	
	local beta`var' = _b[`treatvar']
 	
	boottest `treatvar', weight(webb)  nograph seed(`seed')
local p`var' = r(p)
display r(p)


//get stars based on bootstrapped p values 
glo star_`var' = cond(`p`var'' <0.01, "***", cond(`p`var'' <0.05, "**", cond(`p`var'' <0.1, "*", "")))
local p`var' : di %6.3f `p`var'' 

matrix b`var' = e(b)
matrix v`var' = e(V)
scalar N= e(N)
local N`var' = N


local se`var' = sqrt(v`var'[1,1])

local beta`var' : di %6.3f `beta`var''
local se`var' : di %6.3f `se`var''
local N`var' : di %7.0fc `N`var''



sum `var'
local mean_`var' =r(mean)
local mean_`var' : di %6.2f `mean_`var''
}	 

if "`treatvar'" == "treat_affidavit" {
 
 
 
texdoc init ${rep_root}/output/tables/inv_sup_main_`treatvar'.tex, append force  
tex \hline 
tex \textbf{IV} & `betacomplaints_postc1_3'${star_complaints_postc1_3} & `betacomplaints_postc4_12'${star_complaints_postc4_12}&`betacomplaints_posty2'${star_complaints_posty2}& `betaarrests_postc1_3'${star_arrests_postc1_3} & `betaarrests_postc4_12'${star_arrests_postc4_12}&`betaarrests_posty2'${star_arrests_posty2}& `betatype1_arrests_postc1_3'${star_type1_arrests_postc1_3} & `betatype1_arrests_postc4_12'${star_type1_arrests_postc4_12}&`betatype1_arrests_posty2'${star_type1_arrests_posty2}  \\
tex          & (`secomplaints_postc1_3') & (`secomplaints_postc4_12') & (`secomplaints_posty2') &(`searrests_postc1_3') &(`searrests_postc4_12')&(`searrests_posty2')  &(`setype1_arrests_postc1_3') &(`setype1_arrests_postc4_12')&(`setype1_arrests_posty2')  \\
tex   & [`pcomplaints_postc1_3']& [`pcomplaints_postc4_12']& [`pcomplaints_posty2']& [`parrests_postc1_3']&[`parrests_postc4_12']&[`parrests_posty2']& [`ptype1_arrests_postc1_3']&[`ptype1_arrests_postc4_12']&[`ptype1_arrests_posty2'] \\ 
texdoc close
}
 

texdoc init ${rep_root}/output/tables/inv_sup_main_`treatvar'.tex, append force
tex Dep. var. mean & `mean_complaints_postc1_3'&`mean_complaints_postc4_12'&`mean_complaints_posty2'& `mean_arrests_postc1_3'&`mean_arrests_postc4_12'&`mean_arrests_posty2'& `mean_type1_arrests_postc1_3'&`mean_type1_arrests_postc4_12'&`mean_type1_arrests_posty2'& \\
tex Observations & `Ncomplaints_postc1_3'&`Ncomplaints_postc4_12'&`Ncomplaints_posty2'& `Narrests_postc1_3'&`Narrests_postc4_12'&`Narrests_posty2'& `Ntype1_arrests_postc1_3'&`Ntype1_arrests_postc4_12'&`Ntype1_arrests_posty2'  \\   
tex \hline\hline
tex \end{tabular}

texdoc close

restore 
}

 
 

log close
*clear
